home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.03 Mar 96 / CompleteCodew⁄Binaries folder / MacTech.java < prev    next >
Encoding:
Java Source  |  1996-01-05  |  7.9 KB  |  196 lines  |  [TEXT/ttxt]

  1. /*
  2. In Java, groups of classes are bundled with the package unit.  Whenever another program 
  3. needs to access the public interface to a package, they declare their use with the import 
  4. statement.  In this applet, we are using the Abstract Window Toolkit (awt) and Applet 
  5. classes in the Java Class Hierarchy.  The “*” indicates we need subsequent subclasses as 
  6. well.
  7. */
  8. import java.awt.*;
  9. import java.applet.*;
  10.  
  11. /*
  12. Each compilation unit in Java has exactly one public interface.  In the case where you do 
  13. not define a package explicitly, your classes go into an unnamed “default” package.  Also 
  14. a restriction of the beta Java Development Kit, requires name of the source file containing 
  15. a public interface to match the name of the public class.  In this case, the file that contains 
  16. this source is called MacTech.java.  Below the MacTech class is subclassing the standard 
  17. Applet class and overriding the required Init() method.  By subclassing you gain much 
  18. drawing and event-handling automatically and allowing you to focus on your solution.
  19. */
  20.  
  21. public class MacTech extends java.applet.Applet {
  22.  
  23. /*
  24. The init function is called when a Browser instantiates your instance of the Applet class.  
  25. Any pre-flighting, memory allocation, etc can be performed to determine if the applet has 
  26. all the resources it needs to continue.
  27. */
  28.     public void init() {
  29.         resize(300, 300);                    // drawing area
  30.  
  31. /* The following line may look strange at first to a C/C++ programmer.  But since Java 
  32. does not have a way for the programmer to delete dynamically allocated memory, it is 
  33. OK to not save the result of the new() operator.  Whenever the applet is done using the 
  34. Border Layout object, the garbage collector will recover the memory it allocated.
  35. */
  36.         setLayout(new BorderLayout());
  37.         
  38.         TargetPanel theTarget = new TargetPanel();
  39.         
  40. /* The base drawing context in most C++ application frameworks is the Pane class, the 
  41. equivalent Java class is Panel.  A view hierarachy is created by positioning and creating 
  42. superviews and their sub-Panels.  In the Java AWT various layout objects enforce 
  43. positioning and function as superviews to user inteface elements.  The default border 
  44. layout object carves the display area into equal areas denoted by directional labels 
  45. “North, South, East, West and Center”  Other layout modes let you have tiled rows, 
  46. columns and grid patterns.  The Sun Java example code program “Cards” provides a 
  47. visual map of how each layout pattern is defined.
  48. */
  49.         add("Center", theTarget);                // creates drawing area
  50.         
  51.         add("South",new PopUpMenu(theTarget));        // creates pop-up menu
  52.  
  53. /*
  54. The Applet class implements a Panel interface.  In this case we have defined the 
  55. positioning of our drawing area (theTarget) to be at the center and a Popup Menu Panel 
  56. directly below it.
  57. */
  58.     }
  59. }
  60.  
  61. /*
  62. The following classes implement the functionality of the Panels instantiated by the main 
  63. Applet class.  The TargetPanel’s job is to reflect the current status and updates controlled 
  64. by PopUpMenuPanel class.  Notice how Panels can be event-driven much like OpenDoc 
  65. parts
  66. */
  67.  
  68. class TargetPanel extends Panel {
  69. /*
  70. The “private protected” access specifier may appear odd or in conflict to C++ 
  71. programmers.  In Java, classes in a package have a default access that is very similar to 
  72. the “friend” specifier in C++.  Even the protected access specifier does not prevent other 
  73. classes in a packge from accessing class members.  To appease C++ programmers 
  74. alarmed by this freedom, Sun created the private protected access specifier that behaves 
  75. indentically to the C++ protected access specifier.  Public and private access specifiers 
  76. behave identically to their C++ counterparts.
  77. */
  78.     private protected int curColorIndex;
  79.     private protected Color menuColors[] = { Color.red, Color.green, Color.blue, Color.yellow, 
  80. Color.cyan, Color.magenta, Color.black };
  81.     
  82.     TargetPanel() {
  83.         setBackground(Color.white);
  84.     }
  85.     
  86.     public void SetColorIndex(int inColorIndex) {
  87.         curColorIndex = inColorIndex;
  88.     }
  89.     
  90. /* The Graphics class contains all the QuickDraw-like routines for drawing arcs, ovals, 
  91. rectangles and round rectangles.  The paint method of a Panel is called in response to 
  92. update events only, so take this into consideration when threads or other updating 
  93. changes state information that an update routine depends on.
  94. */
  95.     public void paint(Graphics g){
  96.         g.setColor(menuColors[curColorIndex]);
  97.         g.setPaintMode();
  98.         
  99.         g.fillRect(10,10, 100, 100);
  100.     }
  101.     
  102. /* Panels are equipped to handle all user-input events like keyboard input, mouse 
  103. movement, clicks, and dragging.  Most event processing can be left to the built-in classes.  
  104. In this case, I’m trapping the event sent to a Panel when the browser is closing the 
  105. window.  I could prompt the user for input or alter the handling of the event.
  106. */
  107.     public boolean handleEvent(Event inEvent) {
  108.         switch (inEvent.id) {
  109.             case Event.WINDOW_DESTROY:
  110.             System.exit(0);
  111.             return true;
  112.  
  113.             default:
  114.             return false;
  115.         }
  116.     }
  117. }
  118.  
  119. /*
  120. Rather than implement an entire menu bar for a menu, I decided to opt for the popup 
  121. menu.  In Java, the popup menu interface element is called Choice selection.  In order to 
  122. respond to the user, I let the Panel perform the normal event processing regarding the 
  123. tracking and updating.  By overriding the action method, I am able to respond only to 
  124. changes in the value of the PopUp menu and send an update signal to the target Panel.
  125. */
  126.  
  127. class PopUpMenu extends Panel {
  128.     TargetPanel    thisPanel;
  129.  
  130. /* The constructor for our class receives a reference to Panel that will need updates when 
  131. the value of the PopUp Menu is changed.  Since as of this writing, Java lacks a formal 
  132. resource format, thus all user interface elements must be hard-coded.  Bummer.  It’s 
  133. likely IDE vendors on multiple platforms or perhaps Sun may define a resource format to 
  134. allow strings, menus and control titles to be defined in an external manner.
  135. */
  136.     public PopUpMenu(TargetPanel inMenu) {
  137.         thisPanel = inMenu;
  138.         
  139.         setLayout(new FlowLayout());
  140.         setBackground(Color.lightGray);
  141.     
  142.         Choice colorMenu = new Choice();
  143.  
  144.         colorMenu.addItem("Red");
  145.         colorMenu.addItem("Green");
  146.         colorMenu.addItem("Blue");
  147.         colorMenu.addItem("Yellow");
  148.         colorMenu.addItem("Cyan");
  149.         colorMenu.addItem("Magenta");
  150.         colorMenu.addItem("Black");    
  151.         
  152.         colorMenu.setBackground(Color.lightGray);
  153.         add(colorMenu);
  154.     }
  155.  
  156.     public void paint(Graphics g) {
  157.         Rectangle r = bounds();
  158.     
  159.         g.setColor(Color.lightGray);
  160.  
  161. /* This is one my favorite Graphics toolkit calls.  It paints those 3D-like shadows around 
  162. a standard rectangle to yield a bevel.  Maybe an Apple engineer can add one to Copland.
  163. */
  164.         g.draw3DRect(0, 0, r.width, r.height, false);
  165.     }
  166.  
  167. /* Generally in a layout you may have several user interface elements of various object types.  To 
  168. identify the correct object type you may use Runtime Type Indentication (RTTI) before attempting to 
  169. typecast and access a particular method.  The instanceof operator in Java returns true of an object 
  170. is a proper instance of Class.
  171. */ 
  172.     public boolean action(Event inEvent, Object inObject) {
  173.         if (inEvent.target instanceof Choice) {        // Correct object type - PopUpMenu
  174.             String menuSelection = (String) inObject;
  175.  
  176. /* Since we are responding to changes in the PopUpMenu control, the action method 
  177. gives us the current value.  The value of a Choice object is a String.  This is a little 
  178. inconvenient since we can not use Strings in a switch statement.  Also it duplicates the 
  179. references to the contents of the menu making program maintainence difficult.  I illustrate 
  180. how to process the normal value returned and a way around relying on the String itself.
  181. */
  182.             if (menuSelection.equals("Red"))    // processing standard value returned
  183.                 thisPanel.SetColorIndex(0);
  184.             else    // because of RTTI I can typecast the target object and 
  185.                 // access the menu index directly
  186.                 thisPanel.SetColorIndex(((Choice) inEvent.target).getSelectedIndex());
  187.  
  188. /* 
  189. Sends the update signal to the Drawing Panel so the change can be updated immediately
  190. */
  191.             thisPanel.repaint();
  192.         }
  193.         return true;
  194.     }
  195. }
  196.